package com.ziroom.crm.rock.spring.config;

import com.ziroom.crm.mq.core.config.MqConfig;
import lombok.Data;

import java.util.Objects;

@Data
public class MqRetryConfig {

    /**
     * 消费重试策略
     */
    private MqRetryConsumeConfig consume;

    /**
     * 重新消费(定时介入的消费)重试策略
     */
    private MqRetryConsumeConfig reconsume;

    /**
     * 发送重试策略
     */
    private MqRetrySendConfig send;

    /**
     * 重新发送(定时介入的发送)重试策略
     */
    private MqRetrySendConfig resend;


    public void initMqConfigRetry() {

        if (Objects.nonNull(consume)) {
            MqConfig.setConfigOrDefault(MqConfig.RETRY_CONSUME_POLICY, consume.getPolicy());
            // 覆盖
            MqConfig.setConfigOrDefault(MqConfig.RETRY_CONSUME_COUNT, consume.getCount());
            MqConfig.setConfigOrDefault(MqConfig.RETRY_CONSUME_SECOND_BEFORE_RECONSUME, consume.getSecondBeforeReconsume());

            MqRetryDelayConfig delay = consume.getDelay();
            if (Objects.nonNull(delay)) {
                MqConfig.setConfigOrDefault(MqConfig.RETRY_CONSUME_DELAY_POLICY, delay.getPolicy());
                MqConfig.setConfigOrDefault(MqConfig.RETRY_CONSUME_DELAY_PERIOD, delay.getPeriod());
            }
        }

        if (Objects.nonNull(reconsume)) {
            MqConfig.setConfigOrDefault(MqConfig.RETRY_RECONSUME_POLICY, reconsume.getPolicy());
            MqConfig.setConfigOrDefault(MqConfig.RETRY_RECONSUME_COUNT, reconsume.getCount());

            MqRetryDelayConfig delay = reconsume.getDelay();
            if (Objects.nonNull(delay)) {
                MqConfig.setConfigOrDefault(MqConfig.RETRY_RECONSUME_DELAY_POLICY, delay.getPolicy());
                MqConfig.setConfigOrDefault(MqConfig.RETRY_RECONSUME_DELAY_PERIOD, delay.getPeriod());
            }
        }

        if (Objects.nonNull(send)) {
            MqConfig.setConfigOrDefault(MqConfig.RETRY_SEND_POLICY, send.getPolicy());
            MqConfig.setConfigOrDefault(MqConfig.RETRY_SEND_COUNT, send.getCount());
            MqConfig.setConfigOrDefault(MqConfig.RETRY_SEND_SECOND_BEFORE_RESEND, send.getSecondBeforeReSend());

            MqRetryDelayConfig delay = send.getDelay();
            if (Objects.nonNull(delay)) {
                MqConfig.setConfigOrDefault(MqConfig.RETRY_SEND_DELAY_POLICY, delay.getPolicy());
                MqConfig.setConfigOrDefault(MqConfig.RETRY_SEND_DELAY_PERIOD, delay.getPeriod());
            }
        }

        if (Objects.nonNull(resend)) {
            MqConfig.setConfigOrDefault(MqConfig.RETRY_RESEND_POLICY, resend.getPolicy());
            MqConfig.setConfigOrDefault(MqConfig.RETRY_RESEND_COUNT, resend.getCount());

            MqRetryDelayConfig delay = resend.getDelay();
            if (Objects.nonNull(delay)) {
                MqConfig.setConfigOrDefault(MqConfig.RETRY_RESEND_DELAY_POLICY, delay.getPolicy());
                MqConfig.setConfigOrDefault(MqConfig.RETRY_RESEND_DELAY_PERIOD, delay.getPeriod());
            }
        }
    }
}
